package com.plus.controller;


import com.alibaba.fastjson.JSON;
import com.plus.config.DataSource;
import com.plus.config.DataSourceType;
import com.plus.entity.TApp;
import com.plus.mapper.AppMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import com.plus.common.R;
import com.plus.utils.Func;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

import com.plus.entity.User;
import com.plus.dto.UserDTO;
import com.plus.service.IUserService;

import javax.annotation.Resource;

/**
 * 用户表 控制器
 *
 * 连接池监控
 * http://localhost:8899/druid/datasource.html
 * admin
 * admin
 * @author ht
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/api/user")
@Api(value = "/api/user", tags = "用户表相关接口")
public class UserController {

	@Resource
	private IUserService userService;
	@Resource
	private AppMapper appMapper;


	/**
	 * 主库
	 * @param dto
	 * @return
	 */
	@GetMapping("/page")
	@ApiOperation(value = "分页", notes = "传入user")
	//@DataSource(DataSourceType.MASTER)  //不配置默认访问主库
	public R<IPage<User>> page(UserDTO dto) {
		IPage<User> pages = userService.page(dto);
		return R.data(pages);
	}

	/**
	 * 从库
	 * @param dto
	 * @return
	 */
	@GetMapping("/list")
	@ApiOperation(value = "不分页", notes = "传入user")
	//配置访问从库
	@DataSource(DataSourceType.SLAVE)
	public R<List<User>> list(UserDTO dto) {
		List<User> list = userService.list(dto);
		return R.data(list);
	}

	/**
	 * 从库2
	 * @param id
	 * @return
	 */
	@GetMapping("/list2")
	@ApiOperation(value = "不分页", notes = "传入user")
	//配置访问从库2（sqlserver数据库）
	@DataSource(DataSourceType.SLAVE2)
	public R list2(Integer id) {
		//TApp tApp = appMapper.selectById(id);
		TApp tApp = appMapper.xmlGetModel(id);
		return R.data(tApp);
	}


	@GetMapping("/update")
	@ApiOperation(value = "修改", notes = "传入user")
	public R update(UserDTO dto) {
		return R.data(userService.updateById(dto));
	}

	@GetMapping("/save")
	@ApiOperation(value = "新增", notes = "传入user")
	public R save(UserDTO dto) {
		return R.data(userService.save(dto));
	}

	@DeleteMapping("/remove")
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.data(userService.deleteLogic(Func.toIntList(ids)));
	}

	/**
	 * 详情
	 */
	@PostMapping("/detail")
	@ApiOperation(value = "详情", notes = "传入user")
	public R<User> detail(UserDTO dto) {
		User detail = userService.getOne(dto);
		return R.data(detail);
	}












}
